package com.zyzd.general.module.recyclerview.slidebar;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.gavin.com.library.MyPowerfulStickyDecoration;
import com.gavin.com.library.listener.OnGroupClickListener;
import com.zyzd.commontools.utils.DeviceUtil;
import com.zyzd.commontools.widget.Sidebar;
import com.zyzd.general.R;
import com.zyzd.general.entity.local.UserEntity;
import com.zyzd.general.widget.SwipeItemLayout;
import com.zyzd.retrofit.base.BaseRecyclerFragment;

import butterknife.BindView;

/**
 * Created by lzy on 2018/5/16 0016.
 * description ${快速索引案例}
 */
public class SidebarRecyclerFragment extends BaseRecyclerFragment<SidebarContract.IPresenter, SidebarAdapter, UserEntity> implements SidebarContract.IView {
    @BindView(R.id.sidebar)
    Sidebar mSidebar;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sidebar;
    }

    @Override
    public SidebarAdapter createAdapter() {
        return new SidebarAdapter(getContext());
    }

    @Override
    protected SidebarPresenter createPresenter() {
        return new SidebarPresenter();
    }

    @Override
    protected void initData() {
        mSidebar.setRecyclerView(mRecyclerView);
        mRecyclerView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(getContext()));
    }

    public static SidebarRecyclerFragment getInstance() {
        return new SidebarRecyclerFragment();
    }

    @Override
    public boolean isEnableLoadMore() {
        return false;
    }

    @Override
    public void finishCreateView(Bundle state) {
        super.finishCreateView(state);
        MyPowerfulStickyDecoration decoration = MyPowerfulStickyDecoration.Builder
                .init(new MyPowerfulStickyDecoration.MyPowerGroupListener() {

                    @Override
                    public View getGroupView(int position) {
                        View view = getLayoutInflater().inflate(R.layout.item_group_sidebar, null, false);
                        ((TextView) view.findViewById(R.id.tv_letter)).setText(mAdapter.getItem(position).getInitialLetter());
                        return view;
                    }

                    @Override
                    public void onBindView(int position, boolean isTopFloatView, View groupView) {
                        groupView.findViewById(R.id.view_split).setVisibility(isTopFloatView ? View.VISIBLE : View.GONE);
                        groupView.findViewById(R.id.view_split_short).setVisibility(isTopFloatView ? View.GONE : View.VISIBLE);
                    }

                    @Override
                    public String getGroupName(int position) {
                        return mAdapter.getItem(position).getInitialLetter();
                    }
                })
                .setGroupBackground(Color.WHITE)  //背景色
                .setGroupHeight(DeviceUtil.dpToPx(33))     //高度
//                .setGroupTextColor(ContextCompat.getColor(BaseApplication.getContext(), R.color.color_999))                   //字体颜色
//                .setGroupTextSize(DeviceUtil.spToPx(15))    //字体大小
//                .setDivideColor(ContextCompat.getColor(BaseApplication.getContext(), R.color.color_split))      //分割线颜色
                .setDivideHeight(DeviceUtil.dpToPx(0))     //分割线高宽度
//                .setTextSideMargin(DeviceUtil.dpToPx(27))  //边距
                .setOnClickListener(new OnGroupClickListener() {
                    @Override
                    public void onClick(int position, int id) {

                    }
                })
                .build();
        mRecyclerView.addItemDecoration(decoration);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mSidebar.updateIndex(mAdapter.getItem(((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition()).getInitialLetter());
            }
        });
    }
}
